﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DataApi.Controllers
{
    public class EditController : Controller
    {
        /// <summary>
        /// GET: /Edit/ returns html for an editor based on the type of object
        /// </summary>
        /// <param name="type">The type of object</param>
        /// <param name="name">The name of the object</param>
        /// <param name="id">The id of the object</param>
        /// <param name="embedded">If the request will be embedded in a frame</param>
        /// <param name="mode">The edit mode</param>
        /// <param name="target">Required: The name to use for the div that can be targeted</param>
        /// <param name="loadscripts">Whether or not to load validation and other required javascript files</param>
        /// <returns>Returns the view or embedded html to edit an object</returns>
        public ActionResult Index(String type, String name, String id, String embedded, String mode, String target, String loadscripts)
        {
            // Set the relevant uri data to be available to the view
            ViewData["type"] = type;
            ViewData["name"] = name;
            ViewData["id"] = id;
            ViewData["embedded"] = embedded;
            ViewData["target"] = target;
            ViewData["loadscripts"] = loadscripts;

            // If no mode was set, default to edit mode
            if (!String.IsNullOrEmpty(mode))
                ViewData["mode"] = mode;
            else
                ViewData["mode"] = "Edit";

            /// Designates the type of object we are editing for the view to load
            String edittype = "Edit";

            switch (type)
            {
                case "d":
                    edittype += "Data";
                    break;
                case "v":
                    edittype += "View";
                    break;
                case "m":
                    edittype += "Method";
                    break;
            }

            // If the request will be embeded in a frame
            if (!String.IsNullOrEmpty(embedded) && bool.Parse(embedded))
            {               
                Response.Flush();
                using (StringWriter sw = new StringWriter())
                {
                    // Get partial view from the edittype
                    ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, edittype);
                    // Create a view context with our ViewData
                    ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
                    // Render the view result to string
                    viewResult.View.Render(viewContext, sw);
                    // Return the string as content to the request
                    return Content(sw.GetStringBuilder().ToString());
                }
            }

            // Return the view to be rendered
            return View(edittype);
        }


        /// <summary>
        /// GET: /Create/ returns html for an editor based on the type of object to create an object from
        /// </summary>
        /// <param name="type">The type of object</param>
        /// <param name="name">The name of the object</param>
        /// <param name="embedded">If the request will be embedded in a frame</param>
        /// <param name="target">Required: The name to use for the div that can be targeted</param>
        /// <param name="loadscripts">Whether or not to load validation and other required javascript files</param>
        /// <returns>Returns the view or embedded html to create an object</returns>
        public ActionResult Create(String type, String name, String embedded, String target, String loadscripts)
        {
            return Index(type, name, "", embedded, "Create", target, loadscripts);
        }
    }
}
